package com.hl.hailang.tuwenbianji.fragment;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hj.base.BaseFragment;
import com.hj.controller.Controller;
import com.hj.controller.ControllerParamBean;
import com.hj.controller.ObserverCallback;
import com.hj.recyclerview.BaseAdapter;
import com.hl.apilibrary.ApiHelper;
import com.hl.apilibrary.OnDownLoadCallback;
import com.hl.apilibrary.OnResultCallback;
import com.hl.apilibrary.bean.TTF;
import com.hl.apilibrary.bean.TextBubble;
import com.hl.hailang.R;
import com.hl.hailang.tuwenbianji.adapter.TextBubbleAdapter;
import com.hl.hailang.tuwenbianji.adapter.TtfAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class TextFragment extends BaseFragment {
    private VerticalTabLayout tab_layout;
    private LinearLayout llFontTTF, llFontSize, llFontBg;
    private RecyclerView ttfViews,bubbleViews;
    private TtfAdapter ttfAdapter;
    private TextBubbleAdapter textBubbleAdapter;
    private List<TTF> ttfs = new ArrayList<>();
    private List<TextBubble> bubbles = new ArrayList<>();

    @Override
    public void noticeReceiver(Activity activity, String command, ControllerParamBean params, ObserverCallback callback) {
        super.noticeReceiver(activity, command, params, callback);
    }

    @Override
    protected void onFragmentVisiable() {
        getTTF();
        getTextBubble();
    }

    @Override
    protected void onFragmentIVisiable() {

    }

    @Override
    public void setWindowParams() {

    }

    @Override
    public int setContentViewId() {
        return R.layout.fragment_text;
    }

    @Override
    public void initView() {
        llFontTTF = (LinearLayout) findViewById(R.id.ll_font_ttf);
        llFontSize = (LinearLayout) findViewById(R.id.ll_font_size);
        llFontBg = (LinearLayout) findViewById(R.id.ll_font_bg);
        tab_layout = (VerticalTabLayout) findViewById(R.id.tab_layout);
        ttfViews = (RecyclerView) findViewById(R.id.ttfs);
        bubbleViews = (RecyclerView) findViewById(R.id.text_bubbles);
        ttfAdapter = new TtfAdapter(ttfs);
        ttfViews.setAdapter(ttfAdapter);
        ttfViews.setLayoutManager(new GridLayoutManager(mContext(), 2));

        textBubbleAdapter = new TextBubbleAdapter(bubbles);
        bubbleViews.setAdapter(textBubbleAdapter);
        bubbleViews.setLayoutManager(new GridLayoutManager(mContext(), 2));
    }

    @Override
    public void initData() {
        QTabView tabView = new QTabView(mContext()).setTitle(new QTabView.TabTitle.Builder()
                .setContent("边框")
                .setTextSize(12)
                .setTextColor(getResources().getColor(R.color.black), getResources().getColor(R.color.black))
                .build());
        tabView.setBackgroundResource(R.drawable.bg_white_ffe6e6e6);
        tab_layout.addTab(tabView);

        QTabView tabView1 = new QTabView(mContext()).setTitle(new QTabView.TabTitle.Builder()
                .setContent("字体")
                .setTextSize(12)
                .setTextColor(getResources().getColor(R.color.black), getResources().getColor(R.color.black))
                .build());
        tabView1.setBackgroundResource(R.drawable.bg_white_ffe6e6e6);
        tab_layout.addTab(tabView1);

        QTabView tabView2 = new QTabView(mContext()).setTitle(new QTabView.TabTitle.Builder()
                .setContent("间距")
                .setTextSize(12)
                .setTextColor(getResources().getColor(R.color.black), getResources().getColor(R.color.black))
                .build());
        tabView2.setBackgroundResource(R.drawable.bg_white_ffe6e6e6);
        tab_layout.addTab(tabView2);

        tab_layout.setTabSelected(0);
    }

    @Override
    public void initListener() {
        tab_layout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                if (position == 0) {
                    llFontTTF.setVisibility(View.GONE);
                    llFontBg.setVisibility(View.VISIBLE);
                    llFontSize.setVisibility(View.GONE);
                } else if (position == 1) {
                    llFontTTF.setVisibility(View.VISIBLE);
                    llFontBg.setVisibility(View.GONE);
                    llFontSize.setVisibility(View.GONE);
                } else if (position == 2) {
                    llFontTTF.setVisibility(View.GONE);
                    llFontBg.setVisibility(View.GONE);
                    llFontSize.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        ttfAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<TTF>() {
            @Override
            public void onClick(int position, TTF ttf) {
                downloadTTF(ttf);
            }
        });

        textBubbleAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, Object o) {
                Controller.sendNotification(mActivity(),"bubble",new ControllerParamBean(o),null);
            }
        });
    }

    private void getTextBubble() {
        ApiHelper.getInstance().getTextBubble(mContext(), new OnResultCallback<List<TextBubble>>() {
            @Override
            public void onResult(List<TextBubble> data) {
                if (data != null) {
                    bubbles.clear();
                    bubbles.addAll(data);
                    textBubbleAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int code, String msg) {

            }
        });
    }

    private void getTTF() {
        ApiHelper.getInstance().getTTF(mContext(), new OnResultCallback<List<TTF>>() {
            @Override
            public void onResult(List<TTF> data) {
                if (data != null) {
                    ttfs.clear();
                    ttfs.addAll(data);
                    ttfAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int code, String msg) {

            }
        });
    }

    private void downloadTTF(TTF ttf) {
        String path = mContext().getExternalFilesDir("ttf") + "/" + ttf.getName();
        File file = new File(path);
        if (file.exists()) {

        } else {
            ApiHelper.getInstance().download(mContext(), ttf.getFile_ttf(), path, new OnDownLoadCallback() {
                @Override
                public void onStart() {
                    Log.e("wjh", "onStart");
                }

                @Override
                public void onProgress(long progress, long total) {
                    Log.e("wjh", "onProgress: " + progress + "/" + total);
                }

                @Override
                public void onFinish(String path) {
                    Log.e("wjh", "onFinish: " + path);
                }

                @Override
                public void onError(String msg) {
                    Log.e("wjh", "onError: " + msg);
                }
            });
        }
    }
}
